<?php
/**
 * 获取PID jd.union.open.user.pid.get
 *
 * See: https://union.jd.com/openplatform/api/646
 * User: Ken.Zhang <kenphp@yeah.net>
 * Date: 2019-05-12
 */
class UnionOpenUserPidGetRequest {

    /**
     * 联盟ID
     */
    private $unionId;

    /**
     * 子站长ID
     */
    private $childUnionId;

    /**
     * 推广类型,1APP推广 2聊天工具推广
     */
    private $promotionType;

    /**
     * 子站长的推广位名称，如不存在则创建，不填则由联盟根据母账号信息创建
     */
    private $positionName;

    /**
     * 媒体名称，
     * 即子站长的app应用名称，推广方式为app推广时必填，且app名称必须为已存在的app名称
     */
    private $mediaName;

    /**
     * 请求参数
     */
    public $apiParams = [];

    public function setUnionId($union_id)
    {
        $this->unionId = $union_id;
        $this->apiParams['pidReq']['unionId'] = $union_id;
    }

    public function getUnionId()
    {
        return $this->unionId;
    }

    public function setChildUnionId($child_union_id)
    {
        $this->childUnionId = $child_union_id;
        $this->apiParams['pidReq']['childUnionId'] = $child_union_id;
    }

    public function getChildUnionId()
    {
        return $this->childUnionId;
    }

    public function setPromotionType($promotion_type)
    {
        $this->promotionType = $promotion_type;
        $this->apiParams['pidReq']['promotionType'] = $promotion_type;
    }

    public function getPromotionType()
    {
        return $this->promotionType;
    }

    public function setPositionName($position_name)
    {
        $this->positionName = $position_name;
        $this->apiParams['pidReq']['positionName'] = $position_name;
    }

    public function getPositionName()
    {
        return $this->positionName;
    }

    public function setMediaName($media_name)
    {
        $this->mediaName = $media_name;
        $this->apiParams['pidReq']['mediaName'] = $media_name;
    }

    public function getMediaName()
    {
        return $this->mediaName;
    }

    public function getApiMethodName()
    {
        return 'jd.union.open.user.pid.get';
    }

    /**
     * 解析结果
     *
     * @param   string      $response
     * @throws  Exception
     * @return  mixed
     */
    public function getResult($response)
    {
        $responseKey = str_replace('.', '_', $this->getApiMethodName()) . '_response';

        $response = @json_decode($response, true);
        if (!array_key_exists($responseKey, $response))
            $resp = $response['errorResponse'];
        else {
            $response[$responseKey]['result'] = json_decode($response[$responseKey]['result']);

            $resp = $response[$responseKey];
        }

        return $resp;
    }

}